package cn.kimming.wms.web.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import cn.kimming.wms.anno.RequiredPermission;
import cn.kimming.wms.domain.Employee;
import cn.kimming.wms.domain.JsonMsg;
import cn.kimming.wms.domain.Role;
import cn.kimming.wms.query.EmployeeQueryObject;
import cn.kimming.wms.service.IDepartmentService;
import cn.kimming.wms.service.IEmployeeService;
import cn.kimming.wms.service.IRoleService;
import lombok.Getter;

@Controller("employeeAction")
@Scope("prototype")
public class EmployeeAction extends BaseAction{

	private static final long serialVersionUID = 26674362125140616L;

	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private IDepartmentService departmentService;
	@Autowired
	private IRoleService roleService;

	@Getter
	private Employee employee = new Employee();

	@Getter
	private EmployeeQueryObject qo = new EmployeeQueryObject();

	@Override
	@RequiredPermission("员工列表")
	@InputConfig(methodName="input")
	public String execute() {
		try {
			ActionContext.getContext().put("objs", employeeService.query(qo));
			ActionContext.getContext().put("depts", departmentService.list());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LIST;
	}
	
	@RequiredPermission("员工编辑")
	public String input() {
		try {
			List<Role> roles = roleService.list();
			if (employee.getId() != null) {
				employee = employeeService.get(employee.getId());
				roles.removeAll(employee.getRoles());
			}
			ActionContext.getContext().put("roles", roles);
			ActionContext.getContext().put("depts", departmentService.list());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return INPUT;
	}
	
	@RequiredPermission("员工保存和更新")
	public String saveOrUpdate() {
		if (employee.getId() != null) {
			try {
				employeeService.update(employee);
				addActionMessage("更新成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,更新失败!");
			}
		} else {
			try {
				employeeService.save(employee);
				addActionMessage("保存成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,保存失败!");
			}
		}
		return SUCCESS;
	}

	
	
	@RequiredPermission("员工删除")
	public String delete() {
		try {
			if (employee.getId() != null) {
				employeeService.delete(employee.getId());
				putJSON(new JsonMsg(true, "删除成功"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,删除失败!"));
		}
		return NONE;
	}
	
	public void prepareSaveOrUpdate() throws Exception {
		if (employee.getId()!= null) {
			employee = employeeService.get(employee.getId());
			employee.setDept(null);
		}
		employee.getRoles().clear();
	}
	
	
}
